﻿
namespace bookMaster.AssetSystem.Windows.Forms
{
	/// <summary>
	/// abstract Clientes typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ClientesDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ClientesDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Clientes _currentClientes = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxClientesDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxClientesErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxClientesBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the IdCliente property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdClienteDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the NombreCliente property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNombreClienteDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IdCiudad property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIdCiudadDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the DireccionCliente property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDireccionClienteDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the TelefonoCliente property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxTelefonoClienteDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the EstadoCliente property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxEstadoClienteDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Ciudades> _IdCiudadList;
		
		/// <summary> 
		/// The list of selectable Ciudades
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Ciudades> IdCiudadList
		{
			get {return this._IdCiudadList;}
			set 
			{
				this._IdCiudadList = value;
				this.uxIdCiudadDataGridViewColumn.DataSource = null;
				this.uxIdCiudadDataGridViewColumn.DataSource = this._IdCiudadList;
			}
		}
		
		private bool _allowNewItemInIdCiudadList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Ciudades
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIdCiudadList
		{
			get { return _allowNewItemInIdCiudadList;}
			set
			{
				this._allowNewItemInIdCiudadList = value;
				this.uxIdCiudadDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Clientes> _ClientesList;
				
		/// <summary> 
		/// The list of Clientes to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Clientes> ClientesList
		{
			get {return this._ClientesList;}
			set
			{
				this._ClientesList = value;
				this.uxClientesBindingSource.DataSource = null;
				this.uxClientesBindingSource.DataSource = value;
				this.uxClientesDataGridView.DataSource = null;
				this.uxClientesDataGridView.DataSource = this.uxClientesBindingSource;				
				//this.uxClientesBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxClientesBindingSource_ListChanged);
				this.uxClientesBindingSource.CurrentItemChanged += new System.EventHandler(OnClientesBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnClientesBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentClientes = uxClientesBindingSource.Current as Entities.Clientes;
			
			if (_currentClientes != null)
			{
				_currentClientes.Validate();
			}
			//_Clientes.Validate();
			OnCurrentEntityChanged();
		}

		//void uxClientesBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Clientes"/> instance.
		/// </summary>
		public Entities.Clientes SelectedClientes
		{
			get {return this._currentClientes;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxClientesDataGridView.VirtualMode;}
			set
			{
				this.uxClientesDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxClientesDataGridView.AllowUserToAddRows;}
			set {this.uxClientesDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxClientesDataGridView.AllowUserToDeleteRows;}
			set {this.uxClientesDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxClientesDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxClientesDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientesDataGridViewBase"/> class.
		/// </summary>
		public ClientesDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxClientesDataGridView = new System.Windows.Forms.DataGridView();
			this.uxClientesBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxClientesErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdClienteDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNombreClienteDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIdCiudadDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxDireccionClienteDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxTelefonoClienteDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxEstadoClienteDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIdCiudadBindingSource = new CiudadesBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdCiudadBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxClientesDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxClientesBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxClientesErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxClientesErrorProvider
			// 
			this.uxClientesErrorProvider.ContainerControl = this;
			this.uxClientesErrorProvider.DataSource = this.uxClientesBindingSource;						
			// 
			// uxClientesDataGridView
			// 
			this.uxClientesDataGridView.AutoGenerateColumns = false;
			this.uxClientesDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxClientesDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdClienteDataGridViewColumn,
		this.uxNombreClienteDataGridViewColumn,
		this.uxIdCiudadDataGridViewColumn,
		this.uxDireccionClienteDataGridViewColumn,
		this.uxTelefonoClienteDataGridViewColumn,
		this.uxEstadoClienteDataGridViewColumn			});
			this.uxClientesDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxClientesDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxClientesDataGridView.Name = "uxClientesDataGridView";
			this.uxClientesDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxClientesDataGridView.TabIndex = 0;	
			this.uxClientesDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxClientesDataGridView.EnableHeadersVisualStyles = false;
			this.uxClientesDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnClientesDataGridViewDataError);
			this.uxClientesDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnClientesDataGridViewCellValueNeeded);
			this.uxClientesDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnClientesDataGridViewCellValuePushed);
			
			//
			// uxIdClienteDataGridViewColumn
			//
			this.uxIdClienteDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdClienteDataGridViewColumn.DataPropertyName = "IdCliente";
			this.uxIdClienteDataGridViewColumn.HeaderText = "IdCliente";
			this.uxIdClienteDataGridViewColumn.Name = "uxIdClienteDataGridViewColumn";
			this.uxIdClienteDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdClienteDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdClienteDataGridViewColumn.ReadOnly = true;		
			//
			// uxNombreClienteDataGridViewColumn
			//
			this.uxNombreClienteDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNombreClienteDataGridViewColumn.DataPropertyName = "NombreCliente";
			this.uxNombreClienteDataGridViewColumn.HeaderText = "NombreCliente";
			this.uxNombreClienteDataGridViewColumn.Name = "uxNombreClienteDataGridViewColumn";
			this.uxNombreClienteDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNombreClienteDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNombreClienteDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdCiudadDataGridViewColumn
			//
			this.uxIdCiudadDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdCiudadDataGridViewColumn.DataPropertyName = "IdCiudad";
			this.uxIdCiudadDataGridViewColumn.HeaderText = "IdCiudad";
			this.uxIdCiudadDataGridViewColumn.Name = "uxIdCiudadDataGridViewColumn";
			this.uxIdCiudadDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdCiudadDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdCiudadDataGridViewColumn.ReadOnly = false;		
			//
			// uxDireccionClienteDataGridViewColumn
			//
			this.uxDireccionClienteDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDireccionClienteDataGridViewColumn.DataPropertyName = "DireccionCliente";
			this.uxDireccionClienteDataGridViewColumn.HeaderText = "DireccionCliente";
			this.uxDireccionClienteDataGridViewColumn.Name = "uxDireccionClienteDataGridViewColumn";
			this.uxDireccionClienteDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDireccionClienteDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDireccionClienteDataGridViewColumn.ReadOnly = false;		
			//
			// uxTelefonoClienteDataGridViewColumn
			//
			this.uxTelefonoClienteDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTelefonoClienteDataGridViewColumn.DataPropertyName = "TelefonoCliente";
			this.uxTelefonoClienteDataGridViewColumn.HeaderText = "TelefonoCliente";
			this.uxTelefonoClienteDataGridViewColumn.Name = "uxTelefonoClienteDataGridViewColumn";
			this.uxTelefonoClienteDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTelefonoClienteDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTelefonoClienteDataGridViewColumn.ReadOnly = false;		
			//
			// uxEstadoClienteDataGridViewColumn
			//
			this.uxEstadoClienteDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxEstadoClienteDataGridViewColumn.DataPropertyName = "EstadoCliente";
			this.uxEstadoClienteDataGridViewColumn.HeaderText = "EstadoCliente";
			this.uxEstadoClienteDataGridViewColumn.Name = "uxEstadoClienteDataGridViewColumn";
			this.uxEstadoClienteDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxEstadoClienteDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxEstadoClienteDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdCiudadDataGridViewColumn
			//				
			this.uxIdCiudadDataGridViewColumn.DisplayMember = "NombreCiudad";	
			this.uxIdCiudadDataGridViewColumn.ValueMember = "IdCiudad";	
			this.uxIdCiudadDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIdCiudadDataGridViewColumn.DataSource = uxIdCiudadBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxClientesDataGridView);
			this.Name = "ClientesDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdCiudadBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxClientesErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxClientesDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxClientesBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ClientesDataGridViewEventArgs args = new ClientesDataGridViewEventArgs();
				args.Clientes = _currentClientes;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ClientesDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Clientes	_Clientes;
	
			/// <summary>
			/// the  Entities.Clientes instance.
			/// </summary>
			public Entities.Clientes Clientes
			{
				get { return _Clientes; }
				set { _Clientes = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxClientesDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnClientesDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxClientesDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnClientesDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxClientesDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdClienteDataGridViewColumn":
						e.Value = ClientesList[e.RowIndex].IdCliente;
						break;
					case "uxNombreClienteDataGridViewColumn":
						e.Value = ClientesList[e.RowIndex].NombreCliente;
						break;
					case "uxIdCiudadDataGridViewColumn":
						e.Value = ClientesList[e.RowIndex].IdCiudad;
						break;
					case "uxDireccionClienteDataGridViewColumn":
						e.Value = ClientesList[e.RowIndex].DireccionCliente;
						break;
					case "uxTelefonoClienteDataGridViewColumn":
						e.Value = ClientesList[e.RowIndex].TelefonoCliente;
						break;
					case "uxEstadoClienteDataGridViewColumn":
						e.Value = ClientesList[e.RowIndex].EstadoCliente;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxClientesDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnClientesDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxClientesDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdClienteDataGridViewColumn":
						ClientesList[e.RowIndex].IdCliente = (System.Int32)e.Value;
						break;
					case "uxNombreClienteDataGridViewColumn":
						ClientesList[e.RowIndex].NombreCliente = (System.String)e.Value;
						break;
					case "uxIdCiudadDataGridViewColumn":
						ClientesList[e.RowIndex].IdCiudad = (System.Int32)e.Value;
						break;
					case "uxDireccionClienteDataGridViewColumn":
						ClientesList[e.RowIndex].DireccionCliente = (System.String)e.Value;
						break;
					case "uxTelefonoClienteDataGridViewColumn":
						ClientesList[e.RowIndex].TelefonoCliente = (System.String)e.Value;
						break;
					case "uxEstadoClienteDataGridViewColumn":
						ClientesList[e.RowIndex].EstadoCliente = (System.Int32?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
